#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Collections.Specialized;
using System.Configuration;
using AirWeb.Support.Diagnostics;
using AirWeb.Security;

namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for LoginPage.
	/// </summary>
	public partial class LoginPage : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.LinkButton ForgotPassword;
	
		public LoginPage()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			Response.Expires = -1;
			// Put user code to initialize the page here
			if(Request.QueryString["Logout"] != null)
			{
				FormsAuthentication.SignOut();
				return;
			}

			NameValueCollection config = (NameValueCollection)
				ConfigurationSettings.GetConfig("flashBack/defaultUser");

			if(config != null)
			{
				string user = config["UserName"];
				string pw = config["UserPassword"];

				if(user != null && pw != null)
				{
					Login(user,pw,false);
				}
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
		}

		public void Login(object sender, CommandEventArgs e)
		{
				string un = Request.Form["UserName"];
				string pw = Request.Form["Password"];
				Login(un,pw,Remember.Checked);
		}

		void Login(string i_user, string i_pw, bool i_remember)
		{
			User u = SecurityMgr.Instance.ValidateUser(i_user,i_pw);
			if(u != null)
			{
				Session["KEY_CACHE_CURRENT_USER"] = u; // cache the logged in user to the session
				FormsAuthentication.RedirectFromLoginPage(i_user,i_remember);
			}
			else
			{
				InvalidLogin.Visible = true;
				ApplicationLog.WriteInfo("Invalid login: " + i_user);
			}
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
